package cn.itcast.web.controller.cargo;


import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2021-01-28
 */
@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Reference
    private InvoiceService invoiceService;

    @RequestMapping("/save.do")
    public String save(Invoice invoice, MultipartFile pickupImg) throws Exception {
        invoice.setCompanyId(getLoginUserCompanyId());

        //通过上传文件的大小判断用户是否有上传
        if(pickupImg.getSize()>0){
            //1、把文件上传到七牛云，货物对象保存七牛云的图片的地址
            String url = fileUploadUtil.upload(pickupImg); //返回的就是图片在七牛上的地址
            invoice.setPickupPhoto("http://"+url);
        }

        invoiceService.save(invoice);

        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping("list.do")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //获取companyId
        String loginUserCompanyId = getLoginUserCompanyId();
        //根据companyId查询该公司所有装箱信息
        PageInfo<Invoice> pageInfo = invoiceService.findByPage(pageNum,pageSize,loginUserCompanyId);

        //保存到请求域
        request.setAttribute("pageInfo",pageInfo);

        //返回到list页面
        return "cargo/invoice/invoice-list";
    }

//    /cargo/invoice/toView.do?id="+id
//    @RequestMapping("/toView.do")
//    public String toView(@RequestParam("id") String invoiceListId){
//        PackingList invoiceList = invoiceOrderService.getById(invoiceListId);
//
//        request.setAttribute("invoice",invoiceList);
//
//        return "cargo/invoice/invoice-view";
//    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam("id") String invoiceId){
        ResultInfo resultInfo = invoiceService.delete(invoiceId);

        if (resultInfo.getSuccess()){
            return "redirect:/cargo/invoice/list.do";
        }else {
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }

    }

    @RequestMapping("/finance.do")
    public String Finance(@RequestParam("id") String invoiceId){
        ResultInfo resultInfo = invoiceService.finance(invoiceId);

        if (resultInfo.getSuccess()){
            //若结果为true，data存储到请求域中
            request.setAttribute("finance", resultInfo.getData());

            //跳转到添加委托信息页
            return "cargo/invoice/invoice-finance";
        }else {
            //若结果为fasle，跳转到失败页
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }
    }

    @ResponseBody
    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        //通知浏览器以附件下载的形式处理
        response.setHeader("content-disposition","attachment;filename=invoice.pdf");

        //1. 获取到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/invoice.jasper");

        //2. 根据id查找报运单
        Invoice invoice = invoiceService.findById(id);

        //3. 把报运单的对象转换为map
        Map<String, Object> map = BeanMapUtils.beanToMap(invoice);
        map.put("pay_status", invoice.getStatus() == 1 ?"已支付":"未支付");
        // 创建javabena数据源对象
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map);

        //5. 把报运单的商品存储到数据源中
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,dataSource);

        //6. 把pdf输出即可
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }
}


